package com.africanmoviesmobile.webservice;

import java.io.IOException;
import java.util.Hashtable;

import org.json.JSONArray;
import org.json.JSONObject;
import org.pixmob.httpclient.HttpClientException;

import android.content.Context;

import com.africanmoviesmobile.webservice.inf.Celeb;
import com.africanmoviesmobile.webservice.inf.Filmography;
import com.africanmoviesmobile.webservice.inf.Photo;

public class AmmCeleb extends AmmWebService implements Celeb {
	
	private JSONObject _jsonObject;
	
	public AmmCeleb(JSONObject jsonObject, Context context) {
		super(context);
		_jsonObject = jsonObject;
	}

	public int getId() {
		return _jsonObject.optInt("celeb_id");
	}

	public String getName() {
		return _jsonObject.optString("name");
	}

	public String getBirthName() {
		return _jsonObject.optString("birth_name");
	}

	public String getGender() {
		return _jsonObject.optString("gender");
	}

	public String getBiography() {
		return _jsonObject.optString("biography");
	}

	public String getCountryOfBirth() {
		return _jsonObject.optString("birth_country");
	}

	public String getCountryOfResidence() {
		return _jsonObject.optString("residence_country");
	}

	public String getPlaceOfBirth() {
		return _jsonObject.optString("birthplace");
	}

	public String getDateOfBirth() {
		return _jsonObject.optString("birthday");
	}
	
	public String getDefaultPhoto(){
		return _jsonObject.optString("default_photo");		
	}
	
	public Photo[] getPhotos() throws HttpClientException, IOException, WebServiceException {
		
		Photo[] result = null;
		
		Hashtable<String, String> params = new Hashtable<String, String>();
		params.put("celebId", Integer.toString(getId()));
		
		try {

			JSONObject jo = read("celeb/photo", params);
			
			if ((jo != null) && (jo.length() > 0)) {
				
				JSONArray ja = jo.optJSONArray("data");
				
				if ((ja != null) && (ja.length() > 0)) {
					
					result = new Photo[ja.length()];
					
					for (int i = 0; i < ja.length(); i++) {
						result[i] = new AmmPhoto(ja.optJSONObject(i), getContext());
					}
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
		
	}

	public void getPhotos(final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {

					Photo[] result = getPhotos();

					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
		
	}

	public Filmography[] getFilmographies() throws HttpClientException, IOException, WebServiceException {

		Filmography[] result = null;
		
		Hashtable<String, String> params = new Hashtable<String, String>();
		params.put("celebId", Integer.toString(getId()));
		
		try {

			JSONObject jo = read("celeb/filmography", params);
			
			if ((jo != null) && (jo.length() > 0)) {
				
				JSONArray ja = jo.optJSONArray("data");
				
				if ((ja != null) && (ja.length() > 0)) {
					
					result = new Filmography[ja.length()];
					
					for (int i = 0; i < ja.length(); i++) {
						result[i] = new AmmFilmography(ja.optJSONObject(i), getContext());
					}
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}

	public void getFilmographies(final AsyncCallback listener, final Object state) {
		
		new Thread() {

			public void run() {

				try {

					Filmography[] result = getFilmographies();

					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
		
	}

}
